home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_200 / 293_01 / tgact.c < prev    next >
C/C++ Source or Header  |  1989-08-23  |  5KB  |  177 lines

  1. /*
  2.  * Converts a TARGA-TIPS RGB file into a 256*256 ct file
  3.  * only the upper left part of the RGB file is used
  4.  * the RGB file should be saved from TIPS using the window option
  5.  *
  6.  * Usage: tgact infile outfile headerfile [-l] [-u] [-h]
  7.  *        -l    : lower output density (default = 0)
  8.  *        -u    : upper output density (default = 255)
  9.  *        -h    : number of header blocks (512 bytes) (default = 1)
  10.  */
  11.  
  12. #include <stdio.h>
  13. #include <ctype.h>
  14. #include <math.h>
  15.  
  16. #ifdef MSDOS
  17. typedef int WORD;
  18. #else
  19. typedef short int WORD;
  20. #endif
  21.  
  22. /* allocate all arrays as global */
  23. char  fni[256], fno[256], fnh[256];
  24. WORD   outline[256];
  25. unsigned char inline[256][3];
  26. unsigned char inheader[512];
  27. char *usestr = "Usage: tgact infile outfile headerfile [-l] -[u] [-h]";
  28.  
  29. main(argc, argv)
  30. int argc;
  31. char *argv[];
  32. {
  33.   FILE *fpi, *fpo, *fph;
  34.   int   lower;
  35.   int   upper;
  36.   int   range;
  37.   int   tgaheadi;
  38.   unsigned char tgaheadc;
  39.   int   xsize, ysize;
  40.   int   tgaxsize, tgaysize;
  41.   int   line, pixel;
  42.   int   inlines, inpixels;
  43.   int   i;
  44.   int   header_blocks;
  45.  
  46.   fni[0] = fno[0] = fnh[0] = '\0'; 
  47.   lower = 0;
  48.   upper = 255;
  49.   header_blocks = 1;
  50.   for (argc; argc > 1; argc--) {
  51.     if (**++argv == '-' || **argv == '/') {
  52.       switch (tolower(*(*argv + 1))) {
  53.         case 'l':
  54.           lower = atoi(*argv+2);
  55.           break;
  56.         case 'u':
  57.           upper = atoi(*argv+2);
  58.           break;
  59.         case 'h':
  60.           header_blocks = atoi(*argv+2);
  61.           break;
  62.         default:
  63.           printf("%s\n", usestr);
  64.           exit(0);
  65.           break;
  66.       }
  67.     }
  68.     else {
  69.       if (fni[0] == '\0')
  70.         strcpy(fni, *argv);
  71.       else if (fno[0] == '\0')
  72.         strcpy(fno, *argv);
  73.       else
  74.         strcpy(fnh, *argv);
  75.     }
  76.   }
  77.  
  78.   if (fni[0] == '\0' || fno[0] == '\0' || fnh[0] == '\0') {
  79.     printf("%s\n", usestr);
  80.     exit(0);
  81.   }
  82.  
  83.   if ((fpi = fopen(fni, "rb")) == NULL) {
  84.     printf("Error opening %s\n", fni);
  85.     exit(1);
  86.   }
  87.   if ((fph = fopen(fnh, "rb")) == NULL) {
  88.     printf("Error opening %s\n", fnh);
  89.     exit(1);
  90.   }
  91.   if ((fpo = fopen(fno, "wb")) == NULL) {
  92.     printf("Error opening %s\n", fno);
  93.     exit(1);
  94.   }
  95.  
  96.   /* copy header from ct file */
  97.   for (i = 0; i < header_blocks; i++) {
  98.     fread(inheader, sizeof(unsigned char), 512, fph);
  99.     fwrite(inheader, sizeof(unsigned char), 512, fpo);
  100.   }
  101.  
  102.   range = upper - lower;
  103.   if (range <= 0) {
  104.     printf("lower/upper are out of range (%d)\n", range);
  105.     exit(1);
  106.   }
  107.   if (range < 255)
  108.     printf("lower/upper range gives non optimal contrast (%d)\n", range);
  109.  
  110.   /* test header of tga file */
  111.   fread(&tgaheadc, sizeof(unsigned char), 1, fpi);
  112.   if (tgaheadc != 0)
  113.     printf("Warning Field 1 is not 0 (%d)\n", tgaheadc);
  114.   fread(&tgaheadc, sizeof(unsigned char), 1, fpi);
  115.   if (tgaheadc != 0)
  116.     printf("Warning Field 2 is not 0 (%d)\n", tgaheadc);
  117.   fread(&tgaheadc, sizeof(unsigned char), 1, fpi);
  118.   if (tgaheadc != 2)
  119.     printf("Warning Field 3 is not 2 (%d)\n", tgaheadc);
  120.   for (i = 1; i <= 5; i++) {
  121.     fread(&tgaheadc, sizeof(unsigned char), 1, fpi);
  122.     if (tgaheadc != 0)
  123.       printf("Warning Field 4.%d is not 0 (%d)\n", i, tgaheadc);
  124.   }
  125.   fread(&tgaheadi, sizeof(int), 1, fpi);
  126.   fread(&tgaheadi, sizeof(int), 1, fpi);
  127.  
  128.   fread(&tgaxsize, sizeof(int), 1, fpi);
  129.   fread(&tgaysize, sizeof(int), 1, fpi);
  130.  
  131.   fread(&tgaheadc, sizeof(unsigned char), 1, fpi);
  132.   if (tgaheadc != 24)
  133.     printf("Warning Field 5.5 is not 24 (%d)\n", tgaheadc);
  134.   fread(&tgaheadc, sizeof(unsigned char), 1, fpi);
  135.   if (tgaheadc != 0)
  136.     printf("Warning Field 5.6 is not 0 (%d)\n", tgaheadc);
  137.  
  138.   /* skip lines from input or write empty lines to output */
  139.   inlines = 256;
  140.   if (tgaysize > 256)
  141.     for (line = 256; line < tgaysize; line++)
  142.       for (pixel = 0; pixel < tgaxsize; pixel++)
  143.         fread(inline, sizeof(unsigned char), 3, fpi);
  144.   else if (tgaysize < 256) {
  145.     for (pixel = 0; pixel < 256; pixel++)
  146.       outline[pixel] = lower;
  147.     for (line = tgaysize; line < 256; line++)
  148.       fwrite(outline, sizeof(int), 256, fpo);
  149.     inlines = tgaysize;
  150.   }
  151.   inpixels = 256;
  152.   if (tgaxsize < 256) inpixels = tgaxsize;
  153.  
  154.   /* read in the file one line at a time, convert the density values
  155.      and write to file 256 words */
  156.   for (line = 0; line < inlines; line++) {
  157.     for (pixel = 0; pixel < inpixels; pixel++) {
  158.       fread(inline[pixel], sizeof(unsigned char), 3, fpi);
  159.       outline[pixel] = (float) inline[pixel][0] * (float) range / 255.;
  160.       outline[pixel] += lower;
  161.     }
  162.  
  163.     if (inpixels < 256)
  164.       for ( ; pixel < 256; pixel++)
  165.         outline[pixel] = lower;
  166.     else if (tgaxsize > 256)
  167.       for (pixel = 256; pixel < tgaxsize; pixel++)
  168.         fread(inline, sizeof(unsigned char), 3, fpi);
  169.  
  170.     fwrite(outline, sizeof(int), 256, fpo);
  171.   }
  172.  
  173.   fclose(fpi);
  174.   fclose(fpo);
  175.   fclose(fph);
  176.  
  177. }